import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import "@/assets/less/index.less";
const app = createApp(App);

// 引入pinia
import { createPinia } from 'pinia';
const pinia = createPinia();
app.use(pinia);

// 路由守卫
function isRoute(to){
  return router.getRoutes().filter(item=>item.path === to.path).length > 0
}
router.beforeEach((to, from) => {
  if(to.path !== '/login' && !store.state.token) {
    return { name:"/login"}
  }
  if(!isRoute(to)){
    return {name:'404'}
  }
})
// 动态路由
import { useAllDataStore } from './stores';
const store = useAllDataStore()
store.addMenu(router,"refresh")

app.use(router).mount("#app");

// 完整引入element-plus
import ElementPlus from 'element-plus'
import "element-plus/dist/index.css"
app.use(ElementPlus);


// 引入element-plus/icon
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}
// 引入mockjs
import "@/api/mock.js"

// 全局引入api
import api from "@/api/api";
app.config.globalProperties.$api = api;